<?php
	// print_r($_POST);die;
	require_once "./connect_db.php";
	set_time_limit( 0 );
	ob_implicit_flush();
	$socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
	if ( $socket === false )
	{
		echo "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n";
	}
	if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1))
	{
		echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
	}
	$ok = socket_bind( $socket, '10.133.194.44', 8080 );
	if ( $ok === false )
	{
		echo "socket_bind() failed:reason:" . socket_strerror( socket_last_error( $socket ) );
	}
	socket_getsockname($socket,$local_ip,$local_port);

	$id=$_GET['id'];
    $sql0="SELECT deviceIp,devicePort FROM  send_info WHERE id='$id'";
    $ip_port_result=mysql_query($sql0);
    $nums=mysql_num_rows($ip_port_result);
    for($i=0;$i<$nums;$i++){
        $arr=mysql_fetch_assoc($ip_port_result);
        $ip=$arr['deviceIp'];
        $port=$arr['devicePort'];
    }

    $buf = $_POST['led'];
    // echo $buf;die;
	if($buf=="LED1")
	{
		socket_sendto($socket,"LED1",strlen("LED1"),0,$ip,$port);
	}
	if($buf=="LED0")
	{
		socket_sendto($socket,"LED0",strlen("LED0"),0,$ip,$port);
	}
	if($buf=="LEDL")
	{
		socket_sendto($socket,"LEDL",strlen("LEDL"),0,$ip,$port);
	}
	if($buf=="LEDR")
	{
		socket_sendto($socket,"LEDR",strlen("LEDR"),0,$ip,$port);
	}
	if($buf=="REV")
	{
		socket_sendto($socket,"REV",strlen("REV"),0,$ip,$port);
	}
	if($buf=="FWD")
	{
		socket_sendto($socket,"FWD",strlen("FWD"),0,$ip,$port);
	}
	if($buf=="SWITCH1")
	{
		socket_sendto($socket,"SWITCH1",strlen("SWITCH1"),0,$ip,$port);
	}
	if($buf=="SWITCH0")
	{
		socket_sendto($socket,"SWITCH0",strlen("SWITCH0"),0,$ip,$port);
	}
    if($buf=="修改")
    {
		$update_ip = $_POST['updateIp'];
		$update_port = $_POST['updatePort'];

		$update_message="UPDATE".".".$update_ip.".".$update_port.".";
//		echo $update_message.$ip.$port;die;
        socket_sendto($socket,$update_message,strlen($update_message),0,$ip,$port);
    }
  //   else{
		// socket_sendto($socket,$buf,strlen($buf),0,$ip,$port);

  //   }
	if($_GET['f']==0){
		header("Location:frontend.php/Home/Console/index");
	}else{
		echo "<script>history.go(-1);</script>";
	}
?>
